import Util from '../../utils/util.js';
import {confirmDeliver, getOrderDeliverStat} from "../../api/order";

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {

    lists: [],

    statusList: {
      12: "出餐",
      16: "分拣",
      17: "送达",
    },

    statusList_1: {
      12: "打包",
      16: "分拣",
      17: "送达",
    },

    btnActive: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    const that = this;

    wx.showLoading({
      title: "正在加载..."
    })
    Util.chekWxLogin().then(res => {
      wx.hideLoading()
      that.setData({
        userInfo: res.userInfo,
      })

      let filter = wx.getStorageSync("deliver_filter") || {};
      if (!Util.isEmpty(filter)) {
        filter = JSON.parse(filter);
        that.setData({filter: filter})
      }

      if (filter.status) {
        wx.setNavigationBarTitle({
          title: '确认' + that.data.statusList[filter.status] || '确认订单'
        })
      }

      filter['is_supplier'] = 1;
      wx.showLoading({title: "正在加载..."})
      getOrderDeliverStat(filter).then(res => {
        that.setData({...res.data})
        wx.hideLoading()
      }).catch(errmsg => {
        Util.Tips({title: errmsg}, {tab: 3})
      })
    }).catch(errmsg => {
      Util.Tips({title: errmsg})
    }).finally(() => {
      // wx.hideLoading()
    })

    this.onLoadTabbar();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  confirmDeliver(e) {//分拣
    const that = this

    if (!this.data.btnActive) {
      return Util.Tips({title: "不要重复提交"});
    }

    this.setData({btnActive: false})
    wx.showLoading({title: "正在确认..."})
    confirmDeliver(this.data.filter).then(res => {
      wx.setStorageSync("is_refresh", 1);
      Util.Tips({
        title: that.data.statusList[that.data.filter.status] + "成功",
        icon: "success"
      }, {tab: 3});
      wx.hideLoading();
    }).catch(errmsg => {
      Util.Tips({title: errmsg})
    }).finally(() => {
      that.setData({btnActive: true})
    })

  },

  goBack(e) {//返回
    let pages = getCurrentPages()

    if (pages.length > 1) {
      wx.navigateBack()
    } else {
      wx.switchTab({
        url: "../delivery/index"
      })
    }

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  onLoadTabbar: function () {//底部tabbar返回数据
    const that = this
    Util.checkIphone().then(res => {//检查是否是iphone x/11等
      that.setData({isIphoneX: res})

      wx.createSelectorQuery().in(that).select('.weui-footer')
        .boundingClientRect(function (rect) {
          if (!rect) return;

          let bottom = parseInt(rect.height) + 15,
            cart_bottom = parseInt(rect.height) + 15;
          if (that.data.isIphoneX) {
            bottom += 20;
            cart_bottom += 34
          }
          that.setData({
            bottom: bottom,
            cart_bottom: cart_bottom
          })
        }).exec()
    });
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})